<?php

// 测试
// $paginator = new Paginator(5, 60);
// var_dump($paginator->allUrl());

// 分页类
class Paginator
{
    // 每页显示个数
    protected $count;
    // 一共多少数据
    protected $totalCount;
    // 一共多少页
    protected $totalPage;
    // 当前页
    protected $page;
    // url
    protected $url;

    public function __construct($count, $totalCount)
    {
        $this->count = $count;
        $this->totalCount = $totalCount;
        // 得到总页数
        $this->totalPage = $this->getTotalPage();
        // 得到当前页码
        $this->page = $this->getPage();
        // 得到url
        $this->url = $this->getUrl();
    }

    // 所有 url
    public function allUrl()
    {
        return [
            'first' => $this->first(),
            'prev' => $this->prev(),
            'next' => $this->next(),
            'end' => $this->end()
        ];
    }

    // 首页
    public function first()
    {
        return $this->setUrl('page=1');
    }

    // 上一页
    public function prev()
    {
        if (($this->page - 1) < 1) {
            $page = 1;
        } else {
            $page = $this->page - 1;
        }

        return $this->setUrl('page='  . $page);
    }

    // 下一页
    public function next()
    {
        // 根据当前 page 得到下一页的页码
        if (($this->page + 1) > $this->totalPage) {
            $page = $this->totalPage;
        } else {
            $page = $this->page + 1;
        }

        return $this->setUrl('page='  . $page);
    }

    // 尾页
    public function end()
    {
        return $this->setUrl('page=' . $this->totalPage);
    }

    // 偏移量
    public function limit()
    {
        $offset = ($this->page - 1) * $this->count;
        return $offset . ',' . $this->count;
    }

    // 得到总页数
    protected function getTotalPage()
    {
        return ceil($this->totalCount / $this->count);
    }

    // 得到当前页码
    protected function getPage()
    {
        if (empty($_GET['page']) || $_GET['page'] < 1) {
            return 1;
        } else if ($_GET['page'] > $this->totalPage) {
            return $this->totalPage;
        }
        return $_GET['page'];
    }

    // 得到 url
    protected function getUrl()
    {
        // 获得协议
        $scheme = $_SERVER['REQUEST_SCHEME'];
        // 获得主机名
        $host = $_SERVER['SERVER_NAME'];
        // 获得端口号
        $port = $_SERVER['SERVER_PORT'];
        // 获得路径和请求字符串
        $uri = $_SERVER['REQUEST_URI'];

        // 清空原有 page 参数
        $uriArray = parse_url($uri);
        $path = $uriArray['path'];

        if (!empty($uriArray['query'])) {
            // 将请求字符串变为关联数组
            parse_str($uriArray['query'], $array);
            // 清除掉关联数组中的 page 键值对
            unset($array['page']);
            // 将剩下的参数拼接为请求字符串
            $query = http_build_query($array);
            if ($query != '') {
                $path = $path . '?' . $query;
            }
        }
        return $scheme . '://' . $host . ':' . $port . $path;
    }

    // 设置 url
    protected function setUrl($str)
    {
        if (strstr($this->url, '?')) {
            return $this->url . '&' . $str;
        }
        return $this->url . '?' . $str;
    }
}
